package org.i_plus.controller;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.i_plus.listener.DWRScriptSessionListener;
import org.i_plus.listener.SessionListener;
import org.i_plus.model.services.ChatService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ChatDWR {
	
	/**
     * 測試Browser push
     */
	public void doPush(String fromid,String toid,String chat,HttpServletRequest req){
//		WebApplicationContext context =
//				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
//		ChatService service = (ChatService)context.getBean("ChatService");
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
		ChatService service = (ChatService)context.getBean("ChatService");
		service.insert(toid, fromid, chat);
		
		req.setAttribute("fromId", fromid);//把toid設成request參數 為了給匿名類別用
//		toid是對方的sessionid, fromid是自己的sessionid
//		WebContext webContext = WebContextFactory. get();
//        HttpSession httpsession = webContext.getSession();
		
		//推給自己
				String me = SessionListener.sessionids.get(fromid);//if的開始
		        Browser.withSession(DWRScriptSessionListener.getScriptSessionsBySession(me.substring(0, 32)),//字串要切割一下 因為會有空白 	
		        		new java.lang.Runnable(){
		        		public void run() {    		
		        		Collection<ScriptSession> sessions = Browser.getTargetSessions();  //取得大家的session
		        		ScriptBuffer script = new ScriptBuffer();//對script物件下javascript指令

		        		//toid是對方的sessionid fromid是自己的sessionid
		        		WebContext webContext = WebContextFactory. get();
		                HttpSession httpsession = webContext.getSession();
		                String fromid = httpsession.getId();
		        		                
		        		  String id = null;
		        		  for (ScriptSession session : sessions) {      	        	
		      	        	id = id+"<br>"+session.getId();
		      	        }
		        		
		        		script.appendScript("showMessage(")
		        	        .appendData("k")
		        	        .appendScript(");");
		                    
		        	        for (ScriptSession session : sessions) {      	        	
		        	        	session.addScript(script);
		        	        }
		        		}
		        	
		        });
				
			
		//推給對方
		String sessionid = SessionListener.sessionids.get(toid);
		if(sessionid!=null){//if的開始
        Browser.withSession(DWRScriptSessionListener.getScriptSessionsBySession(sessionid.substring(0, 32)),//字串要切割一下 因為會有空白 	
        		new java.lang.Runnable(){
        		public void run() {    		
        		Collection<ScriptSession> sessions = Browser.getTargetSessions();  //取得大家的session
        		ScriptBuffer script = new ScriptBuffer();//對script物件下javascript指令

        		//toid是對方的sessionid fromid是自己的sessionid
        		WebContext webContext = WebContextFactory. get();
                HttpSession httpsession = webContext.getSession();
                String fromid = httpsession.getId();
        		              
        		  String id = null;
        		  for (ScriptSession session : sessions) {      	        	
      	        	id = id+"<br>"+session.getId();
      	        }
        		
        		script.appendScript("showMessage(")
        	        .appendData( webContext.getHttpServletRequest().getAttribute("fromId") )
        	        .appendScript(");");
                    
        	        for (ScriptSession session : sessions) {      	        	
        	        	session.addScript(script);
        	        }
        		}
        	
        });
		
	}
	}//if的結束

}


